<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2017-06-30
 * Time: 09:10
 */

namespace app\admin\controller;


use app\src\base\logic\BaseLogic;
use app\src\sunsun\adt\logic\AdtDeviceLogic;
use app\src\sunsun\aph300\logic\Aph300DeviceLogic;
use app\src\sunsun\aq806\logic\Aq806DeviceLogic;
use app\src\sunsun\cp1000\logic\Cp1000DeviceLogic;
use app\src\sunsun\filterVat\logic\FilterVatDeviceLogic;
use app\src\sunsun\heatingRod\logic\HeatingRodDeviceLogic;
use app\src\sunsun\water_pump\logic\WaterPumpDeviceLogic;
use think\Controller;

class FactoryDevice extends Controller
{


    public function create_cp1000(){
        if($this->request->isGet()){
            $prefix = $this->request->param('prefix','');
            dump('最近一次插入数据库的did= '.$this->getLastDid(new Cp1000DeviceLogic(),$prefix));
            return $this->by_display();
        }
        $prefix = $this->request->param('prefix','S05X');
        $start = $this->request->param('start',101);
        $cnt = $this->request->param('cnt',1);
        $this->generate($prefix,$start,$cnt,new Cp1000DeviceLogic());
    }

    public function create_water_pump(){
        if($this->request->isGet()){
            $prefix = $this->request->param('prefix','');
            dump('最近一次插入数据库的did= '.$this->getLastDid(new WaterPumpDeviceLogic(),$prefix));
            return $this->by_display();
        }
        $device_type = $this->request->param('device_type','JDP-6000');
        $prefix = $this->request->param('prefix','S05X');
        $start = $this->request->param('start',101);
        $cnt = $this->request->param('cnt',1);
        $this->generate($prefix,$start,$cnt,new WaterPumpDeviceLogic(),['device_type'=>$device_type]);
    }

    public function create_adt(){
        if($this->request->isGet()){
            $prefix = $this->request->param('prefix','');
            dump('最近一次插入数据库的did= '.$this->getLastDid(new AdtDeviceLogic(),$prefix));
            return $this->by_display();
        }
        $device_type = $this->request->param('device_type','ADT-C');
        $prefix = $this->request->param('prefix','S06C');
        $start = $this->request->param('start',101);
        $cnt = $this->request->param('cnt',1);
        $this->generate($prefix,$start,$cnt,new AdtDeviceLogic(),['device_type'=>$device_type]);
    }

    protected function getLastDid(BaseLogic $logic,$prefix=''){
        $map = [];
        $map['did'] = ['like',$prefix.'%'];
        if(!empty($prefix)){
            $result = $logic->getInfo($map,'did desc');
        }else{
            $result = $logic->getInfo($map,'id desc');
        }
        return $result['info']['did'];
    }

    public function index(){
        return $this->by_display();
    }

    public function create_filter(){
        if($this->request->isGet()){
            $prefix = $this->request->param('prefix','');
            dump('最近一次插入数据库的did= '.$this->getLastDid(new FilterVatDeviceLogic(),$prefix));
            return $this->by_display();
        }
        $prefix = $this->request->param('prefix','S01C');
        $start = $this->request->param('start',101);
        $cnt = $this->request->param('cnt',1);
        $limit_country = $this->request->param('limit_country','');
        $this->generate($prefix,$start,$cnt,new FilterVatDeviceLogic(),['limit_country'=>$limit_country]);
    }

    public function create_heating_rod(){
        if($this->request->isGet()){
            $prefix = $this->request->param('prefix','');
            dump('最近一次插入数据库的did= '.$this->getLastDid(new HeatingRodDeviceLogic(),$prefix));
            return $this->by_display();
        }
        $prefix = $this->request->param('prefix','S02C');
        $start = $this->request->param('start',201);
        $cnt = $this->request->param('cnt',1);
        $this->generate($prefix,$start,$cnt,new HeatingRodDeviceLogic());
    }


    public function create_aq806(){
        if($this->request->isGet()){
            $prefix = $this->request->param('prefix','');
            dump('最近一次插入数据库的did= '.$this->getLastDid(new Aq806DeviceLogic(),$prefix));
            return $this->by_display();
        }
        $prefix = $this->request->param('prefix','S03C');
        $start = $this->request->param('start',301);
        $cnt = $this->request->param('cnt',1);
        $this->generate($prefix,$start,$cnt,new Aq806DeviceLogic());
    }

    public function create_aph300(){
        if($this->request->isGet()){
            $prefix = $this->request->param('prefix','');
            dump('最近一次插入数据库的did= '.$this->getLastDid(new Aph300DeviceLogic(),$prefix));
            return $this->by_display();
        }

        $prefix = $this->request->param('prefix','S04C');
        $start = $this->request->param('start',301);
        $cnt = $this->request->param('cnt',1);
        $this->generate($prefix,$start,$cnt,new Aph300DeviceLogic());
    }

    private function generate($prefix='S00',$start=100,$count=40,BaseLogic $logic,$extra=[]){
        $len = 10;
        $entity = [];
        for ($i = 0 ; $i < $count; $i++) {
            $did = str_pad(''.($start + $i),$len,'0',STR_PAD_LEFT);
            $did = $prefix.$did;
            $pwd = $this->generate_password();
            $oneEntity = [
                'did'=>$did,
                'ver'=>'v1.0',
                'pwd'=>$pwd,
                'ctrl_pwd'=>'12345678',
                'hb'=>120,
                'last_login_time'=>0,
                'tcp_client_id'=>'',
            ];
            if(is_array($extra)){
                $oneEntity = array_merge($oneEntity,$extra);
            }
            array_push($entity,$oneEntity);
        }
        $result = $logic->addAll($entity);
        $info = $result['info'];
        if($result['status']){
            $allEntity = [];
            foreach ($info as $vo){
                if(method_exists($vo,"toArray")){
                    array_push($allEntity,$vo->toArray());
                }
            }
            foreach ($allEntity as $entity){
                echo $entity['did'].' '.$entity['pwd'].'<br/>';
            }
            return $allEntity;
        }else{
            var_dump($info);
        }
    }

    private function generate_password( $length = 8 ) {
        // 密码字符集，可任意添加你需要的字符
        $chars = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789';
        $password = '';
        for ( $i = 0; $i < $length; $i++ )
        {
            $password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
        }
        return $password;
    }

    private function by_display($theme='default',$file=false){
        if(!empty($file)){
            return $this->fetch($theme.'/'.$file);
        }else{
            return $this->fetch($theme."/". request()->controller().'/'.request()->action());
        }
    }
}